/*!
 * 树列表对象.
 * ecp.qzz.treelist.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.object","qzz.treenode"], function() {
  
  var qzz = window.qzz;
  
  qzz.treelist = qzz.Class(qzz.object, {
    /**
     * 创建初始化函数.
     */
    _create:function(){
      this.base();
        this.className = "TreeList";
        this.logInfo("treelist.create");
        //根节点对象
        this.rootNode = new qzz.treenode();
        //根节点层次号
        this.rootNode.level = -1;
        //序号
        this.rootNode.scrIndex = 0;
        //初始化当前节点
        this.curNode = this.rootNode;
        //合计行用.
        this.rootNode.data = {};
        //临时节点
        this.subNode = new qzz.treenode();
        //层次
        this.subNode.level = -2;
        //序号
        this.subNode.scrIndex = -1;
        //小计
        this.subNode.data = {};
        //过滤栈 {fieldName, all, sel}
	    this.filterStack = [];
        //初始化时，是否需要排序.
        this.initSort = true;
        //是否刷新
        this._updates = [];
        //初始化起始、结束状态
        this.eof = false;
        this.bof = true;
        //默认初始化类型.
        this.initType = this.getOption().initType || "code";
        //初始化层次结构字段
        this.itemCode = this.getOption().itemCode || "bm";
        //主键
        this.keyName = this.itemCode;
        //编码
        this.getOption().itemCode =  this.itemCode;
        //初始化层次结构字段.
        this.getOption().idkey = this.getOption().idkey || "id";
        
        this.getOption().capkey = this.getOption().capkey || "text";
        //this.idKey = "id";
        this.getOption().pidkey = this.getOption().pidkey || "pid";
        //序号对应JSON
        this.dataIndexJson = {};
        //节点序号        
        this.itemIndex = 0;
        //复选择
        this.getOption().multiSelect = this.getBooleanValue(this.getOption().multiSelect, this.multiSelect);
        this.multiSelect = this.getOption().multiSelect;
        //锁定自动选择.
        this.lockAutoCheck = false;
        //树形结构
        this.isTree = true;
        //自动选择样式
        this.autoCheckType = {
            autoCheckParent:false,
            autoUnCheckParent:false,
            autoCheckAllParent:false,
            autoUnCheckAllParent:false,
            autoCheckAllParentExt:false,
            autoUnCheckAllParentExt:false,
            autoCheckChild:false,
            autoUnCheckChild:false,            
            autoCheckAllChild:false,
            autoUnCheckAllChild:false,
            autoCheckBrother:false,
            autoUnCheckBrother:false
        };
        //选择类型
        this.checkType = {
              all:"all",
              unall:"unall",
              rever:"rever"
        }
        
        this.modelState = {
            DEFAULT:1,
          NEW:4,// 新增: 模型节点被新增后处于该状态
          MODIFY:8,// 修改: 模型节点被修改后处于该状态
          DELETE:2//,// 删除: 此状态暂时只有占位作用，删除的内容已移动至模型删除集中,模型节点被删除后处于该状态
          //CHILD_MODIFY:64, // 子对象被修改
      }
        
        this._lockLog = false;        
      //changeSet
      this._changeSet = {"deleteList":[],"modify":{}};
        
        this.filtered = false;
        //如果父节点被过滤，则整个子孙分支过滤，否则，只要有一个子孙节点没有被过滤，才父节点不过滤.
        this._filterAll = this.getBooleanValue(this.getOption().filterAll, false);
      },
      /**
       * 设置编码.
       */
      setItemCode:function(key) {
        this.itemCode = key;
        this.initType = "code";
      },
      /**
       * 设置构造树类型.
       */
      setIDPID:function(idKey, pidKey) {
        this.getOption().idkey = idKey;
        this.getOption().pidkey = pidKey;
        this.initType = "idpid";
      },
      /**
       * 复选.
       * @param value 状态
       */
      setMultiSelect:function(value) {
        this.getOption().multiSelect = value;
        this.multiSelect = this.getOption().multiSelect;
      },
      /**
       * 自动选择父节点.
       * @param value 状态
       */
      autoCheckParent:function(value) {    
            this.autoCheckType.autoCheckParent = this.getBooleanValue(value, false);
      },
      /**
       * 自动取消选择父节点.
       * @param value 状态
       */
      autoUnCheckParent:function(value) {
            this.autoCheckType.autoUnCheckParent = this.getBooleanValue(value, false);
      },
      /**
       * 自动选择所有父节点.
       * @param value 状态
       */
      autoCheckAllParent:function(value) {
            this.autoCheckType.autoCheckAllParent = this.getBooleanValue(value, false);
      },
      /**
       * 自动取消选择所有父节点.
       * @param value 状态
       */
      autoUnCheckAllParent:function(value) {
            this.autoCheckType.autoUnCheckAllParent = this.getBooleanValue(value, false);
      },
      /**
       * 自动选择所有父节点.
       * @param value 状态
       */
      autoCheckAllParentExt:function(value) {
            this.autoCheckType.autoCheckAllParentExt = this.getBooleanValue(value, false);
      },
      /**
       * 自动取消选择所有父节点.
       * @param value 状态
       */
      autoUnCheckAllParentExt:function(value) {
            this.autoCheckType.autoUnCheckAllParentExt = this.getBooleanValue(value, false);
      },
      /**
       * 自动选择子节点.
       * @param value 状态
       */
      autoCheckChild:function(value) {
            this.autoCheckType.autoCheckChild = this.getBooleanValue(value, false);
      },
      /**
       * 自动取消选择子节点.
       * @param value 状态
       */
      autoUnCheckChild:function(value) {
            this.autoCheckType.autoUnCheckChild = this.getBooleanValue(value, false);
      },
      /**
       * 自动选择所有子节点.
       * @param value 状态
       */
      autoCheckAllChild:function(value) {
            this.autoCheckType.autoCheckAllChild = this.getBooleanValue(value, false);
      },
      /**
       * 自动取消选择所有子节点.
       * @param value 状态
       */
      autoUnCheckAllChild:function(value) {
            this.autoCheckType.autoUnCheckAllChild = this.getBooleanValue(value, false);
      },
      /**
       * 自动选择同级节点.
       * @param value 状态
       */
      autoCheckBrother:function(value) {
            this.autoCheckType.autoCheckBrother = this.getBooleanValue(value, false);
      },
      /**
       * 自动取消选择同级节点.
       * @param value 状态
       */
      autoUnCheckBrother:function(value) {
            this.autoCheckType.autoUnCheckBrother = this.getBooleanValue(value, false);
      },
      /**
       * 处理自动选择
       * @param {object} node 指定选择的节点.
       * @ignore
       */
      doAutoCheck:function(node) {
        var node = node || this.curNode;
        if(node.checkState) return;
        node.checkState = true;
        if(node.pNode != null) {
            node.pNode.childCheckCount ++;
        }
        if(this.lockAutoCheck) return;
        if(node.pNode != null) {
	        //自动选择同级
	        if(this.autoCheckType.autoCheckBrother) {
	          var pNode = node.pNode;
	          for(var i = 0; i < pNode.count; i++) {
	            if(pNode.cNode[i].enable) {
	                pNode.cNode[i].checkState = true;
	            }
	          }
	          pNode.childCheckCount = pNode.count;
	        }
	        //自动选择上级
	        if(this.autoCheckType.autoCheckAllParent) {
	          //下级全选， 才会自动选择上级
	          var pNode = node.pNode;
	          while(pNode != null && !pNode.checkState) {
	            if(pNode.enable && pNode.childCheckCount == pNode.count) {
	              pNode.checkState = true;
	              if(pNode.pNode != null) {
	                  pNode.pNode.childCheckCount ++;
	              }
	            } else {
	              break;
	            }
	            pNode = pNode.pNode;
	          }
	        } else if(this.autoCheckType.autoCheckAllParentExt) {
	          //只要选择下级就会自动选择所有上级
	          var pNode = node.pNode;
	          while(pNode != null && !pNode.checkState) {
	            if(pNode.enable) {
	              pNode.checkState = true;
	              if(pNode.pNode != null) {
	                  pNode.pNode.childCheckCount ++;
	              }
	            }
	            pNode = pNode.pNode;
	          }
	        } else if(this.autoCheckType.autoCheckParent) {
	          //直接上级节点
	          if(node.level >= 0 && !node.pNode.checkState) {
	            var pNode = node.pNode;
	            if(pNode.enable && pNode.childCheckCount == pNode.count) {
	                pNode.checkState = true;
	                if(pNode.pNode != null) {
	                    pNode.pNode.childCheckCount ++;
	                }
	            }
	          }
	        }
        }
        //自动选择下级
        if(this.autoCheckType.autoCheckAllChild) {
          //所有下级节点
          var ui = this.beginUpdate(true);
          var cn = this.getCurNode();
          this.select(node);
          node.childCheckCount = node.count;
          this.next(true);
          while (!this.isEof() && this.getLevel() > node.level) {
            if(this.curNode.enable) {
              this.curNode.checkState = true;
              this.curNode.childCheckCount = this.curNode.count;
            }
            this.next(true);
          }
          this.select(cn);
          this.endUpdate(false, ui);
        } else if(this.autoCheckType.autoCheckChild) {
          //直接下级节点
          for(var i = 0; i < node.count; i++) {
            if(node.cNode[i].enable) {
                node.cNode[i].checkState = true;
            }
          }
          node.childCheckCount = node.count;
        }        
      },
      /**
       * 自动取消选择下级.
       * @param {object}node 指定选择的节点.
       * @ignore
       */
      doAutoUnCheck:function(node) {
        var node = node || this.curNode;
        if(!node.checkState) return;
        node.checkState = false;
        if(node.pNode != null) {
            node.pNode.childCheckCount --;
        }
        if(this.lockAutoCheck) return;
        if(node.pNode != null) {
	        //自动取消选择同级
	        if(this.autoCheckType.autoUnCheckBrother) {
	          var pNode = node.pNode;
	          for(var i = 0; i < pNode.count; i++) {
	            if(pNode.cNode[i].enable) {
	                pNode.cNode[i].checkState = false;
	            }
	          }
	          pNode.childCheckCount = 0;
	        }
	        //自动取消选择上级
	        if(this.autoCheckType.autoUnCheckAllParent) {
	          //只要有一个节点取消， 就自动取消上级
	          var pNode = node.pNode;
	          while(pNode != null && pNode.checkState) {
	            if(pNode.enable) {
	              pNode.checkState = false;
	              if(pNode.pNode != null) {
	                  pNode.pNode.childCheckCount --;
	              }
	            }
	            pNode = pNode.pNode;
	          }
	        } else if(this.autoCheckType.autoUnCheckAllParentExt) {
	          //所有节点取消了才取消上级
	          var pNode = node.pNode;
	          while(pNode != null && pNode.checkState) {
	            if(pNode.enable && pNode.childCheckCount == 0) {
	              pNode.checkState = false;
	              if(pNode.pNode != null) {
	                  pNode.pNode.childCheckCount --;
	              }
	            } else {
	              break;
	            }
	            pNode = pNode.pNode;
	          }
	        } else if(this.autoCheckType.autoUnCheckParent) {
	          //直接上级节点
	          if(node.level >= 0 && node.pNode.checkState) {
	            if(node.pNode.enable) {
	               node.pNode.checkState = false;
	               if(pNode.pNode != null) {
	                   node.pNode.pNode.childCheckCount--;
	               }
	            }
	          }
	        }        
        }
        //自动取消选择下级
        if(this.autoCheckType.autoUnCheckAllChild) {
          //所有下级节点
          var ui = this.beginUpdate(true);
          var cn = this.getCurNode();
          this.select(node);
          node.childCheckCount = 0;
          this.next(true);
          while (!this.isEof() && this.getLevel() > node.level) {    
            if(this.curNode.enable) {
              this.curNode.checkState = false;
              this.curNode.childCheckCount = 0;
            }
            this.next(true);
          }
          this.select(cn);
          this.endUpdate(false, ui);
        } else if(this.autoCheckType.autoUnCheckChild) {
          //直接下级节点
          for(var i = 0; i < node.count; i++) {
            if(node.cNode[i].enable) {
              node.cNode[i].checkState = false;
            }
          }
          node.childCheckCount = 0;
        }
      },
      /**
       * 判断当前是否选择
       * @param node 节点
       * @return 返回状态
       */      
      isChecked:function(node) {
        var node = node || this.curNode;
        return node.checkState;
      },
      /**
       * 判断当前是否选择
       * @param node 节点
       * @return 返回状态
       */
      isSelected:function(node) {
        return this.isChecked(node);
      },
      /**
       * 设置选择
       * @param node 节点
       * @return 返回状态
       */
      setCheck:function(node) {
        var node = node || this.curNode;
        this.doAutoCheck(node);
      },
      /**
       * 设置选择
       * @param node 节点
       * @return 返回状态
       */
      setSelect:function(node) {
        this.setCheck(node);
      },
      /**
       * 取消选择
       * @param node 节点
       * @return 返回状态
       */
      setUnCheck:function(node) {
        var node = node || this.curNode;
        this.doAutoUnCheck(node);
      },
      setUnSelect:function(node) {
        this.setUnCheck(node);
      },
      /**
       * 锁住自动选择.
       * @param value 状态
       */
      setLockAutoCheck:function(value) {
        if(this.isEmpty(value)) {
          value = true;
        }
        if(typeof value === "string") {
          value = Boolean(value);
        }
        this.lockAutoCheck = value;
      },
      /**
       * 是否到了结束节点.
       * @return 结束状态
       */
      isEof:function() {
        return this.getAllCount() == 0 || this.eof;
      },
      /**
       * 是否到起始节点.
       * @return 起始状态
       */
      isBof:function() {
        return this.getAllCount() == 0 || this.bof;
      },
      /**
       * 获取根节点.
       * @return 节点对象.
       */
      getRootNode:function() {
        return this.rootNode;
      },
      /**
       * 获取增减总数.
       */
      getRecordChanged:function() {
    	  var cs = this._changeSet.modify;
    	  var re = 0;
    	  for(var key in cs) {
    		  var node = cs[key];
    		  if((node.data.modelState & this.modelState.NEW) > 0) {
    			  re++;
    		  }
    	  }
    	  return re - this._changeSet.deleteList.length;
      },
      /**
       * 添加节点.
       * @param node 当前节点
       * @param pNode 父节点
       * @param focus 是否选择
       * @return 返回状态
       */
      addNode:function(node, pNode, focus){
        if(node instanceof qzz.treenode) {
            if(focus == null) {
                focus = true;
            } 
            if(this.rootNode.getCount() === 0) {
        	    pNode = this.rootNode;
            } else if(pNode == null) {
        	    pNode = this.curNode;
            } else if(!(pNode instanceof qzz.treenode)) {
                return false;
            }
            var addState = false;
            if(this.sortType == "down") {
            	addState = pNode.insertNode(0, node);
            } else {
            	addState = pNode.addNode(node);
            }
	        //添加处理
	        if(addState) {
	          //生成脏数据集
	          if(node.data == null) {
	            node.data = {};
	          }
	          if(this._lockLog !== true) {
	            node.data.modelState = this.modelState.NEW;
	            //从变更列表中删除
	            if(this._changeSet.modify[node.recNo] == null) {
	              this._changeSet.modify[node.recNo] = node;
	            }
	          }
	          if(focus) {
	            this.curNode = node;
	              //处理节点变更事件
	              if(this.isUpdate() && typeof this.onChanged == "function") {
	                try {
	                    this.onChanged(this);
	                } catch(e) {
	                  this.logInfo("onChanged error:" + e.message);
	                }
	              }// if changed  
	              if(this.isUpdate() && this.hasbind("onEdited") === true) {
	                  this.trigger("onEdited",["add"]);
	              }
	          } // if focus
	        } // if addNode
            return true;
        } else {
          return false;
        }
      },
      /**
       * 插入节点.
       * @param index 插入序号
       * @param node 当前节点
       * @param pNode 父节点
       * @param focus 是否选择
       * @return 返回状态
       */
      insertNode:function(index, node, pNode, focus) {
        if(node instanceof qzz.treenode) {
          if(focus == null) {
            focus = true;
          }
          if(this.rootNode.getCount === 0) {
            //如果只有根节点，则直接添加到根节点之下
            if(this.rootNode.addNode(node)) {
              if(focus) {
                  this.curNode = node;
                  if(node.data == null) {
                  node.data = {};
                }
                if(this._lockLog !== true) {
                  node.data.modelState = this.modelState.NEW;
                  //从变更列表中删除
                  if(this._changeSet.modify[node.recNo] == null) {
                    this._changeSet.modify[node.recNo] = node;
                  }
                }
                  //处理节点变更事件
                  if(this.isUpdate() && typeof this.onChanged == "function") {
                    try {
                        this.onChanged(this);
                    } catch(e) {
                      this.logInfo("onChanged error:" + e.message);
                    }
                  }// if changed  
                  if(this.isUpdate() && this.hasbind("onEdited") === true) {
                      this.trigger("onEdited",["insert"]);
                  }
              }
            }
          } else {
            //如果有数据，则添加到pNode之下
            if(pNode === null) {
                pNode = this.curNode.pNode;
            } else if(!(pNode instanceof qzz.treenode)) {
              return false;
            }
            if(pNode.insertNode(index, node)) {
              //生成脏数据集
              //生成脏数据集
              if(node.data == null) {
                node.data = {};
              }
              if(this._lockLog !== true) {
                node.data.modelState = this.modelState.NEW;
                //从变更列表中删除
                if(this._changeSet.modify[node.recNo] == null) {
                  this._changeSet.modify[node.recNo] = node;
                }
              }
              if(focus) {
                  this.curNode = node;
                  //处理节点变更事件
                  if(this.isUpdate() && typeof this.onChanged == "function") {
                    try {
                        this.onChanged(this);
                    } catch(e) {
                      this.logInfo("onChanged error:" + e.message);
                    }
                  }// if changed  
                  if(this.isUpdate() && this.hasbind("onEdited") === true) {
                      this.trigger("onEdited",["insert"]);
                  }
              }
            }
          }
          return true;
        }
        return false;
      },
      /**
       * 删除节点.
       * @param index 节点序号
       * @param pNode 父节点对象
       * @return 返回状态
       */
      delNode:function(index, pNode) {
        var delCurNode = (index === null || index == this.curNode.index);
        //如果没有设置index 则默认为删除当前节点
        if(delCurNode) {
          index = this.curNode.index;
        }
        if(pNode === null) {
          if(delCurNode) {
                pNode = this.curNode.pNode;
          } else {
            pNode = this.curNode;
          }
        } else if(!(pNode instanceof qzz.treenode)) {
          return false;
        }
        //删除后，重新定位当前节点
        if(index >= 0 && index < pNode.getCount()) {
          if(delCurNode) {
            if(index > 0) {
                this.curNode = pNode.getNode(index - 1);
                //移到上一个
                while(this.curNode.getCount() > 0 && this.curNode.expanded) {
                  this.curNode = this.curNode.getNode(this.curNode.getCount() - 1);
                }
            } else if(pNode.level > 0) {
              this.curNode = pNode;
            } else if(pNode.getCount() > 1){
              this.curNode = pNode.getNode(index + 1);
              this.curNode.scrIndex = 1;
            } else {
              this.curNode = pNode;
            }
          }
          
          var cn = pNode.getNode(index);
          var item = cn.data;
          var recNo = cn.recNo;
          cn = null;
          //执行删除
            pNode.delNode(index);
            if(this._lockLog !== true) {
            	//如果不是刚刚新增加的行，删除掉的话就直接去除日志
            	if(item.modelState !== this.modelState.NEW) {
                    //生成脏数据集
	                item.modelState = this.modelState.DELETE;
	                this._changeSet.deleteList.push(item);
                }
	            //从变更列表中删除
	            if(this._changeSet.modify[recNo] != null) {
	                delete this._changeSet.modify[recNo];
	            }
	            item = null;
            }
          
            //处理节点变更事件
          if(this.isUpdate() && typeof this.onChanged == "function") {
            try {
                this.onChanged(this);
            } catch(e) {
              this.logInfo("onChanged error:" + e.message);
            }
          }// if changed  
          if(this.isUpdate() && this.hasbind("onEdited") === true) {
              this.trigger("onEdited",["delete"]);
          }
          return true;
        }
        return false;
      },
      /**
       * 获取子节点.
       * @param index 节点序号
       * @param node 父节点
       * @return 返回子节点
       */
      getChildNode:function(index, node) {
        var node = node || this.curNode;
        return node.cNode[index];
      },
      /**
       * 获取节点序号.
       * @param node 节点
       * @return 返回序号
       */
      getChildIndex:function(node) {
        var node = node || this.curNode;
        return node.index;
      },
      /**
       * 下一个节点.
       * @param all 遍历所有节点标志
       * @return 返回状态
       */
      next:function(all) {
        var isNext = false;
        var scrIndex = this.curNode.scrIndex;
        var cac = this.curNode.allCount;
        if(this.curNode.getCount() > 0) {
          //如果有子节点
          if(all || this.curNode.expanded) {
            //如果是展开状态，则返回第一个子节点
            this.curNode = this.getChildNode(0);//this.curNode.cNode[0];
            isNext = true;
            if(this.curNode.scrIndex != scrIndex + 1) {
                       this.curNode.scrIndex = scrIndex + 1;
            }            
          } else {
            //find next brother
              //如果没有子节点，则获取下一个兄弟节点
            var pNode = this.curNode.pNode;
            var cIndex = this.getChildIndex();//this.curNode.index;
            while(pNode != null) {
              if(cIndex < pNode.getCount() - 1) {
                //如果当前节点是父节点的最后一个节点，则找父节点的兄弟节点
                this.curNode = this.getChildNode(cIndex + 1, pNode);//pNode.cNode[cIndex + 1];
                isNext = true;
                break;
              } else {
                //返回兄弟节点
                cIndex = this.getChildIndex(pNode);//pNode.index;
                pNode = pNode.pNode;
              }
            }
            //计算全局序号
            if(isNext && this.curNode.scrIndex != scrIndex + cac) {
                        this.curNode.scrIndex = scrIndex + cac;
                    }                        
            pNode = null;
            cIndex = null;            
          }          
        } else {
          //find next brother
            //如果没有子节点，则获取下一个兄弟节点
          var pNode = this.curNode.pNode;
          var cIndex = this.getChildIndex();//this.curNode.index;
          while(pNode != null) {
            if(cIndex < pNode.getCount() - 1) {
              //如果当前节点是父节点的最后一个节点，则找父节点的兄弟节点
              this.curNode = this.getChildNode(cIndex + 1, pNode);//pNode.cNode[cIndex + 1];
              isNext = true;
              break;
            } else {
              //返回兄弟节点
              cIndex = this.getChildIndex(pNode);//pNode.index;
              pNode = pNode.pNode;
            }
          }
          //计算全局序号
          if(isNext && this.curNode.scrIndex != scrIndex + cac) {
                    this.curNode.scrIndex = scrIndex + cac;
          }
          pNode = null;
          cIndex = null;          
        }        
        //处理节点变更事件
        if(this.isUpdate() && isNext && typeof this.onChanged == "function") {
          try {
              this.onChanged(this);
          } catch(e) {
            this.logInfo("onChanged error:" + e.message);
          }
        }
        //标识状态
        this.bof = false;
        this.eof = !isNext;
        return isNext;
      },
      /**
       * 最后一个节点.
       * @param node 指定节点
       */
      lastNode:function(node) {
        if(node == null) {
          node = this.rootNode;
        }
        while(node.getCount() > 0 && node.expanded) {
          node = this.getChildNode(node.getCount() - 1, node);//node.cNode[node.getCount() - 1];
        }
        return node;
      },
      /**
       * 最后一个节点
       * @return 状态
       */
      last:function() {
        var recNo = this.curNode.recNo;
          this.curNode = this.lastNode(this.rootNode);
        if(this.isUpdate() && recNo != this.curNode.recNo && typeof this.onChanged == "function" ) {
          try {
              this.onChanged(this);
          } catch(e) {
            this.logInfo("onChanged error:" + e.message);
          }
        }
        this.curNode.scrIndex = this.getAllCount();
        this.bof = false;
        this.eof = true;
        return true;
      },
      /**
       * 上一个节点.
       * @return 状态
       */
      prior:function() {
        var isPrior = false;
        if(this.getChildIndex() == 0) {//this.curNode.index == 0) {
          //如果是第一个子节点，则返回父节点
          if(this.curNode.level > 0) {
            this.curNode = this.curNode.pNode;
            isPrior = true;
          }
        } else {
          //否则返回前一个兄弟节点的最后一个子节点
          var pNode = this.curNode.pNode;
          this.curNode = this.lastNode(this.getChildNode(this.getChildIndex() - 1, pNode));//pNode.cNode[this.curNode.index - 1]);
          isPrior = true;
        }
        //滚动状态
        if(this.isUpdate() && isPrior && typeof this.onChanged == "function") {
          try {
              this.onChanged(this);
          } catch(e) {
            this.logInfo("onChanged error:" + e.message);
          }
        }
        this.bof = !isPrior;
        this.eof = false;
        return isPrior;
      },
      /**
       * 移到第一个节点.
       * @return 状态
       */
      first:function() {
        var recNo = this.curNode.recNo;
        if(this.rootNode.getCount() > 0) {
          this.curNode = this.getChildNode(0, this.rootNode);//this.rootNode.cNode[0];
        }
        //滚动状态
        if(this.isUpdate() && recNo != this.curNode.recNo && typeof this.onChanged == "function") {
          try {
              this.onChanged(this);
          } catch(e) {
            this.logInfo("onChanged error:" + e.message);
          }
        }
        this.bof = true;
        this.eof = false;
        return true;
      },
      /**
       * 移动到父节点.
       * @param {number} level 移到层次
       * @return 状态
       */
      gotoParent:function(level) {
        if(this.curNode.level > 0) {
          //如果不是顶节点，则移到父节点上
          if(level == null) {
              this.curNode = this.curNode.pNode;
          } else {
            var pNode = this.curNode;
            while(pNode != null && pNode.level >= 0 && pNode.level > level) {
              pNode = pNode.pNode;
            }
            if(pNode != null) {
              this.curNode = pNode;              
            }
          }
          //滚动状态
          if(this.isUpdate() && typeof this.onChanged == "function") {
            try {
                this.onChanged(this);
            } catch(e) {
              this.logInfo("onChanged error:" + e.message);
            }
          }
          this.bof = false;
            this.eof = false;
        }
        return true;
      },
      /**
       * 移动到子节点.
       * @return 状态
       */
      gotoChild:function(index, pNode) {
        if(pNode == null) {
          pNode = this.curNode;
        }
        if(index >= 0 && index < pNode.getCount()) {          
          this.curNode = this.getChildNode(index, pNode);//this.curNode.cNode[index];
          //滚动状态
          if(this.isUpdate() && typeof this.onChanged == "function") {
            try {
                this.onChanged(this);
            } catch(e) {
              this.logInfo("onChanged error:" + e.message);
            }
          }
          this.bof = false;
            this.eof = false;
        }
        return true;
      },
      /**
       * 选择指定节点.
       * @param node 节点
       */
      select:function(node) {
        if(node instanceof qzz.treenode && node.recNo != this.curNode.recNo) {
          this.curNode = node;
          //滚动状态
          if(this.isUpdate() && typeof this.onChanged == "function") {
            try {
                this.onChanged(this);
            } catch(e) {
              this.logInfo("onChanged error:" + e.message);
            }
          }          
        }
        this.bof = false;
        this.eof = false;
      },
      /**
       * 展开事件.
       */
      expand:function() {
        this.curNode.expand();
        this.curRecNo = this.curNode.recNo;
      },
      /**
       * 判断是否展开.
       */
      isExpand:function(node) {
        var node = node || this.curNode;
        if(node != null) {
            return node.expanded === true;
        } else {
        	return false;
        }
      },
      /**
       * 刷新前.
       */
      beginUpdate:function(useIndex) {
    	  if(this._updates == null) {
    		  this._updates = [];
    	  }
    	  if(useIndex === true) {
	    	  if(this._updateIndex == null || this._updateIndex > 10) {
	    		  this._updateIndex = 0;
	    	  }	    	  
	    	  this._updateIndex++
	    	  this._updates[this._updateIndex] = false;
    	  } else {
    		  this._updates[0] = false;
    	  }
          return this._updateIndex;
      },
      /**
       * 更新处理.
       */
      isUpdate:function() {
    	  var res = true;
    	  for(var i = 0; i <= 10; i ++) {
    		  res = res && this._updates[i] !== false;
    	  }
    	  return res;
      },
      /**
       * 清空更新控制标志.
       */
      clearUpdate:function() {
    	this._updates = [];  
      },
      /**
       * 刷新后.
       * @param update 刷新状态
       */
      endUpdate:function(update, updateIndex) {
    	  this._updates[updateIndex || 0] = true;
          if(update != false && this.isUpdate() && this.curRecNo != this.curNode.recNo && typeof this.onChanged == "function") {
	          try {
	              this.onChanged(this);
	          } catch(e) {
	            this.logInfo("onChanged error:" + e.message);
	          }
	      }
      },
      /** 
      * 异步处理全展开函数. 
      * @param node 节点 
      * @param cNode 当前节点
      * @param limit 限制数量
      * @param callBack 回调 
      */ 
      _asynFullExpand:function(node, cNode, visibleCount, limit, callBack){ 
        this.logInfo("_asynFullExpand:" + node.recNo); 
        var i = 0; 
        var ui = this.beginUpdate(true); 
        //缓存当前节点 
        var tmpNode = this.curNode; 
        //设置起始节点 
        this.select(cNode); 
        //循环处理所有节点， 当前level大于总节点level时循环 
        while(!this.isEof() && i < limit 
            && (this.getLevel() > node.level || node.recNo == this.getRecNo())) { 
          if(!this.curNode.expanded) { 
            this.curNode.expanded = true; 
            this.curNode.visibleCount = this.curNode.allCount; 
          } 
          i++; 
          this.next(); 
        } 
        //如果没有结束， 且当前level大于总节点level时，继续 
        if(!this.isEof() && this.getLevel() > node.level) { 
          cNode = this.curNode; 
          this.select(tmpNode); 
          this.endUpdate(false, ui); 
          //如果还没有处理结束，则异步再调用自己. 
          var _this = this; 
          setTimeout(function(){
            _this._asynFullExpand(node, cNode, visibleCount, limit, callBack); _this = null
            },10); 
        } else { 
          //层层往上累加 
          if(node.level >= 0) {
            //获取新增加显示的数量 
            var vc = node.allCount - visibleCount; 
            var pNode = node.pNode; 
            while(pNode != null && pNode.expanded && vc > 0) { 
              pNode.visibleCount += vc; 
              pNode = pNode.pNode; 
            } 
          } 
          //根节点处理 
          node.expanded = true; 
          node.visibleCount = node.allCount; 
          //还原选择 
          this.select(tmpNode); 
          this.endUpdate(false, ui); 
          //如果处理结束，则调用回调函数 
          if(typeof callBack == "function") { 
            callBack(); 
          } 
        } 
      }, 
      /** 
      * 全部展开节点.
      * @param {treeNode} node 根节点，为空则默认指当前节点
      * @param {function} callBack 回调函数 
      */ 
      fullExpand:function(node, callBack) { 
        if(typeof node === "function") { 
          callBack = node; 
          node = null; 
        } 
        var ui = this.beginUpdate(true); 
        //移动到第一个节点 
        if(node == null) { 
          node = this.rootNode; 
          node.scrIndex = 0;
        } 
        this.endUpdate(false, ui); 
        var vc = node.expanded ? node.visibleCount : 1;
        //调用异步处理函数 
        this._asynFullExpand(node, node, vc, 100, callBack); 
        //重新定位 
        if(node.level < 0) { 
          this.first(); 
        } else if(node.level != null){ 
          this.select(node); 
        } 
      },
      /**
       * 删除整个树节点.
       * @param {treeNode} node 根节点，为空则默认指当前节点
       * @param {function} callBack 回调函数
       */
      deleteAllItem:function(node, callBack) {
        if(typeof node === "function") { 
          callBack = node; 
          node = null; 
        } 
        if(node == null) {
          node = this.curNode;
        }
        if(node.level >= 0) {
          //有子节点的时候
          if(node.getCount() > 0) {
            var me = this;
            //全展开
            this.fullExpand(node, function() {
              var ui = me.beginUpdate(true);
              //选择最后子节点.
              var ln = me.lastNode(node);
              me.select(ln);
              //从后往前删除，
              while(node.level < me.getLevel()) {
                me.delRecord();
              }
              //删除根节点.
              me.delRecord();
              me.endUpdate(false, ui);
              //调用回调
              if(typeof callBack === "function") {
                callBack();
              }
            });            
          } else {
            //没有子节点，直接删除
            var ui = me.beginUpdate(true);
            me.delRecord();
            me.endUpdate(false, ui);
            //直接回调
            if(typeof callBack === "function") {
              callBack();
            }
          }
        } else {
          //直接回调
          if(typeof callBack === "function") {
            callBack();
          }
        }
      },
      /**
       * 收缩节点.
       */
      collapse:function() {
        this.curNode.collapse();
      },
      /**
       * 异步全部收缩节点.
       * @name _asnyFullCollapse
       * @param node 根节点
       * @param cNode 当前节点
       * @param limitCount 异步数量
       * @param callBack 回调函数
       * @ignore
       */
      _asnyFullCollapse:function(node, cNode, limitCount, callBack) {  
        if(this.getAllCount() > 0) {
          if(node == null) {
            node = this.rootNode;            
          }
          if(cNode == null) {
            cNode = node.cNode[0];
          }
          this.logInfo("_asnyFullCollapse:" + node.recNo);          
          var ui = this.beginUpdate(true);
          this.select(cNode);
          var i = 0;
          //循环处理排序
          while(!this.isEof() && this.getLevel() > node.level && i < limitCount) {
            var cNode = this.getCurNode();
            cNode.expanded = false;
            cNode.visibleCount = cNode.count + 1;
            cNode = null;
            this.next(true);
            i++;
          }
          this.endUpdate(false, ui);
          if(!this.isEof() && this.getLevel() > node.level) {
            //如果没有处理完，则异步再调用
            var _this = this;
            cNode = this.getCurNode();
            setTimeout(function(){_this._asnyFullCollapse(node, cNode, limitCount, callBack); _this = null}, 10);
          } else {
            node.visibleCount = node.count + 1;
            //回调函数
            if(typeof callBack == "function") {
                callBack.apply(this);
            }
          }
          } else {
              if(typeof callBack === "function") {
                  callBack();
              }
          }
      },
      /**
       * 全部收缩节点.
       */
        fullCollapse:function(callBack) {
        	//直接用遍历函数控制全收缩的状态.
        	this.each(function(cNode){
	    		cNode.expanded = false;
	    		cNode.visibleCount = cNode.count + 1;
        	}, function() {
        		this.rootNode.visibleCount = this.rootNode.count + 1;
            	if(typeof callBack === "function") {
        			callBack();
        		}
        	});
      },
      /**
       * 定位到根节点.
       * @parem 返回根节点
       */
      getTopNode:function() {
        if(this.getAllCount() > 0) {
          return this.rootNode.getNode(0);
        } else {
          return this.rootNode;
        }
      },
      /**
       * 获取节点
       * @param index 节点序号 
       * @param pNode 父节点
       * @param focus 定位状态
       * @return 返回节点
       */
      getNode:function(index, pNode, focus) {
        if(focus == null) {
          focus = true;
        }
        if(!(pNode instanceof qzz.treenode)) {
          pNode = this.curNode;
        }
        return pNode.getNode(index);        
      },
      /**
       * 获取当前节点.
       * @return 返回当前节点
       */
      getCurNode:function(){
        return this.curNode;
      },
      /**
       * 获取父节点
       */
      getParentNode:function(node) {
    	   if(node == null) {
    		   node =  this.dataSet.getCurNode();
    	   }	    	 
    	   return node.pNode; 
      },
      /**
       * 获取节点的子节点数量.
       * @param node 节点对象
       * @return 返回节点的子节点数量
       */
      getCount:function(node) {
        if(node == null) {
          node = this.curNode;
        }
        return node.getCount();
      },
      /**
       * 获取所有节点数量.
       */
      getAllCount:function() {
        return this.rootNode.allCount - 1;
      },
      length:function() {
        return this.getAllCount();
      },
      /**
       * 获取所有可见的节点数量
       */
      getVisibleCount:function() {
        return this.rootNode.visibleCount - 1;
      },
      /**
       * 异步清空节点.
       * @param topNode 顶节点
       * @param node 当前节点
       * @param pNode 父节点
       * @param nodeStack 节点堆栈
       */
      _asynEmptyNode:function(topNode, node, pNode, nodeStack, callBack) {
        this.logInfo("_asynEmptyNode");
        //判断、初始化节点堆栈
        if(nodeStack == null) {
          nodeStack = [];
        }
        //判断、初始化当前节点.
        if(node == null) {
          node = topNode;
        }
        //顶节点有子节点
        if(topNode.count > 0) {
          var curNode = node;
          if(pNode == null) {
            pNode = topNode;
            curNode = topNode.cNode[0];
          }
          var index;
          var count = 0;
          //循环注销节点
          while (pNode != null && count < 10) {
            if(curNode.count > 0) {
              //如果有子节点，则入栈
                if(pNode != null) {
                  nodeStack.push(pNode);
                }
              pNode = curNode;
                curNode = curNode.cNode[0];
            } else {
              //没有子节点，则直接注销
              index = curNode.index; //this.getIndex(curNode);//
              curNode.destroy();
              //处理后续节点
              if(index < pNode.count - 1) {
                curNode = pNode.cNode[index + 1];
              } else {
                pNode.count = 0;
                curNode = pNode;
                if(nodeStack.length > 0) {
                    pNode = nodeStack.pop();
                } else {
                  pNode = null;
                }
              }
            }
            count ++;
          }
          node = null;
          if(pNode != null) {
            //如果没有处理完，则异步调用注销函数
            var _this = this;
            setTimeout(function(){_this._asynEmptyNode(topNode, curNode, pNode, nodeStack, callBack), _this = null;}, 10);
          } else {    
            //如果处理完成，把最后节点注销
            curNode.destroy();
            curNode = null;
            topNode = null;
            nodeStack = null;
            if(typeof callBack === "function") {
              callBack();
            }
          }    
        } else {
          //没有子节点，则直接注册当前节点
          topNode.destroy();
          topNode = null;
          nodeStack = null;
          if(typeof callBack === "function") {
            callBack();
          }
        }
      },
      /**
       * 清空树列表节点.
       */
      empty:function(callBack) {         
        var tmpTopNode = this.rootNode;
        //创建新的根节点
        this.rootNode = new qzz.treenode();
        this.rootNode.level = -1;
        this.curNode = this.rootNode;
        this._updates = [];
        this.eof = false;
        this.bof = true;
        //释放旧的根节点，及其子节点
        this._asynEmptyNode(tmpTopNode, null, null, null, callBack);
      },
      /**
       * 返回id值.
       */
      getId:function(node) {
    	  if(node == null) {
    		  node = this.curNode;
    	  }
    	  return node.id;
      },  
      /**
       * 返回文本值.
       */
      getText:function(node) {
    	  if(node == null) {
    		  node = this.curNode;
    	  }
    	  return node.text;
      },
      /**
       * 返回编码.
       */
      getItemCode:function(node) {
    	  if(node == null) {
    		  node = this.curNode;
    	  }
    	  return node.fullcode;
      },
      setId:function(value, node) {
    	  if(node == null) {
    		  node = this.curNode;
    	  }
    	  node.data[this.getOption().idkey] = value;
    	  node.id = value;
      },      
      setText:function(txt, node) {
    	  if(node == null) {
    		  node = this.curNode;
    	  }
    	  node.data[this.getOption().capkey] = value;
    	  node.text = value;
      },
      /**
       * 返回扩展区数据.
       */
      getValue:function(fieldName, node) {
    	  if(fieldName instanceof qzz.treenode) {
    		  node = fieldName;
    		  fieldName = null;
    	  }
    	  if(node == null) {
    		  node = this.curNode;
    	  }
    	  if(node.data != null && fieldName != null) {
    		  var paths = "";
    		  if(this.getOption().fullPath === true) {
    			  var cnode = node.pNode;
    			  while(cnode.level >= 0) {
    				  paths = cnode.data[fieldName] + "." + paths;
    				  cnode = cnode.pNode;
    			  }
    		  }
    		  return paths + node.data[fieldName];
    	  } else if(fieldName == null){
    		  var paths = "";
    		  if(this.getOption().fullPath === true) {
    			  var cnode = node.pNode;
    			  while(cnode.level >= 0) {
    				  paths = cnode.id + "." + paths;
    				  cnode = cnode.pNode;
    			  }
    		  }
    		  return paths + node.id;
    	  } else {
    		  return "";
    	  }
      },
      /**
       * 异步初始化节点列表.
       * @param itemList 节点数组
       * @param pCode 父节点编码
       * @param pNode 父节点
       * @param index 节点序号
       * @param callBack 回调函数
       */
      _asynSetItemList:function(itemList, pCode, pNode, index, insertIndex, callBack) {
        this.logBegin("_asynSetItemList:field:" + "pCode:" + pCode + ",index:" + index);
        var ui = this.beginUpdate(true);
        var selNode = this.curNode;
        var len = itemList.length;
        var i = index, count = 0, cItem, cNode, cCode; 
        //循环增加节点
        while(i<len && count < 50) {          
          cItem = itemList[i];
          //创建新节点
          cNode = new qzz.treenode();
        cNode.id = cItem[this.getOption().idkey];
        cNode.text = cItem[this.getOption().capkey];        
        cNode.scrIndex = i + 1;
        //if(this.storeData) {
        cNode.data = cItem;
        //}
        //开始创建
        if(this.isTree == false) {
          if(insertIndex != null && insertIndex >= 0 && insertIndex < pNode.getCount()) {
              this.insertNode(insertIndex, cNode, pNode);
              insertIndex++;
          } else {
              this.addNode(cNode, pNode);
          }
        } else if(this.initType === "idpid") {
          //id,pid树创建
          var pid = cItem[this.getOption().pidkey];
          var id = cItem[this.getOption().idkey];
          cNode.cid = id;
          cNode.pid = pid;
          if(pNode == null) {
              pNode = this.rootNode;
            }
          while(pNode.cid != cNode.pid && pNode.level >= 0) {
            pNode = pNode.pNode;
          }
          if(insertIndex != null && insertIndex >= 0 && insertIndex < pNode.getCount()) {
            this.insertNode(insertIndex, cNode, pNode);
            insertIndex++;
          } else {
              this.addNode(cNode, pNode);
          }
          pNode = cNode;
        } else {
          cCode = String(cItem[this.itemCode]);
          cNode.fullcode = cCode;
            var clen = cCode.length;
            var plen = pCode.length;
            var code = cNode.fullcode || "";
          if(plen === 0) {            
              //如果父节点的编码为空,则直接添加
            if(insertIndex != null && insertIndex >= 0 && insertIndex < pNode.getCount()) {
              this.insertNode(insertIndex, cNode, pNode);
              insertIndex++;
            } else {
                this.addNode(cNode, pNode);
            }
              code = code.replace(pNode.fullcode, "");
              pCode = cCode;
              pNode = cNode;
            } else if(cCode.indexOf(pCode) === 0 &&　plen < clen) {
              //如果子编码包含父编码，并长于父编码，则添加
              if(insertIndex != null && insertIndex >= 0 && insertIndex < pNode.getCount()) {
                this.insertNode(insertIndex, cNode, pNode);
                insertIndex++;
              } else {
                  this.addNode(cNode, pNode);
              }
                code = code.replace(pNode.fullcode, "");
                pCode = cCode;
                pNode = cNode;
            } else {
              //寻找父节点
              pNode = pNode.pNode;
              while(pNode != null) {
                pCode = pNode.fullcode || "";
                plen = pCode.length;
                if(plen == 0 || plen < clen && cCode.indexOf(pCode) === 0) {
                  break;
                }
                pNode = pNode.pNode;
              }
              if(pNode == null) {
                pNode = this.rootNode;
              }
              if(plen != clen) {
                //找到父节点，则添加节点
                if(insertIndex != null && insertIndex >= 0 && insertIndex < pNode.getCount()) {
                  this.insertNode(insertIndex, cNode, pNode);
                  insertIndex++;
                } else {
                    this.addNode(cNode, pNode);
                }
                  code = code.replace(pNode.fullcode, "");
                  pCode = cCode;
                  pNode = cNode;
              }    
            }
          cNode.code = code;
        }         
          count ++;
          i ++;
        }        
        this.endUpdate(false, ui);
        this.curNode = selNode;
        this.logEnd("end _asynSetItemList count:" + count);
        if(i < len) {
          //如果添加未完成，则调用异步函数继续添加
          var _this = this;
          setTimeout(function(){_this._asynSetItemList(itemList, pCode, pNode, i, insertIndex, callBack); _this = null}, 5);
        } else {
          //如果添加完成，则调用回调函数
          if(typeof callBack == "function") {
            callBack();
          }
          cItem = null;
          cNode = null;
          pNode = null;
        }  
      },
      /**
       * 获取当前节点的层次号.
       */
      getLevel:function() {
        return this.curNode.level;
      },
      /**
       * 获取某节点新编码.
       * @param pNode 父节点
       */
      getNewCode:function(pNode) {
        if(pNode.maxCode == null) {
          pNode.maxCode = 0;
          pNode.codeLen = 4;
          var c = pNode.getCount();
          if(c > 0) {
            var mc = pNode.getNode(c - 1).code;
            pNode.codeLen = mc.length;
            pNode.maxCode = parseInt(mc, 10);
          }
        }        
        pNode.maxCode ++;
         var nextCode = pNode.maxCode + "";
         var nlen = nextCode.length;
         for(var j = nlen; j < pNode.codeLen; j++) {
           nextCode = "0" + nextCode;
         }
         return nextCode;
      },
      /**
       * 重新生成编码.
       * @param itemAry 节点数组
       * @param pNode 父节点
       * @return 返回状态
       */
      newItemCode:function(itemAry, pNode) {
        if(this.isArray(itemAry) && itemAry.length > 0) {
          var pCode = pNode.fullcode;
          var fcode = itemAry[0][this.itemCode];
          //获取新编码
          var nextCode = this.getNewCode(pNode);
          itemAry[0][this.itemCode] = nextCode;
          //循环处理
          for(var i = 1; i < len; i++) {
            var cc = itemAry[i][this.itemCode];
            if(cc.indexOf(fcode) >= 0) {
              cc = cc.replace(fcode, pCode + nextCode);
            } else {
              fcode = cc;
              cc = pCode + this.getNewCode(pNode);
            }
            itemAry[i][this.itemCode] = cc;
          }          
            return true;
        }
        return false;
      },
      /**
       * 跟据编码字段进行数组排序.
       * @param {Person[]} itemList 数组
       * @param storeKeyIndex 保存key序号状态
       * @return 返回排好序的列表
       */
      sortItemList:function(itemList, storeKeyIndex) {
        this.logBegin("begin sort item list");
        if(this.initSort == false || this.isTree == false) {
          return itemList;
        }
        var itemListJson = {};
        var itemListAry = [];
        var item = null;
        if(this.initType === "code") {
          var len = itemList.length;
          var sortarr = [];
          //循环把序号存到数组及json中，
          for(var i = 0; i < len; i++) {
            item = itemList[i];
            var keyVal = item[this.itemCode];
            sortarr[i] = keyVal;
            itemListJson[keyVal] = item;
          }
          //调用数组的Array排序功能
          sortarr.sort();          
          //循环把数据填充到返回的数组中
          for(var i =0;i<len;i++) {
            var keys = sortarr[i];
            itemListAry.push(itemListJson[keys]);
          }
          sortarr = null;
        } else {          
          var len = itemList.length;
          var mJS = {};
          var fJS = {};
          //循环把序号存到数组及json中，
          for(var i = 0; i < len; i++) {
            item = itemList[i];
            var id = item[this.getOption().idkey];
            var pid = item[this.getOption().pidkey];
            var n = {"id":id,"pid":pid,"index":i,"state":0,"cn":[]};
            mJS[id] = n;
            if(mJS[pid] != null) {
              mJS[pid].cn.push(n);
              mJS[id] = n;
              n.state = 1;
            } else if(fJS[pid] != null) {
              fJS[pid].cn.push(n);
              mJS[id] = n;
              n.state = 1;
            } else {
              fJS[id] = n;
            }
          }
          //把最后的一层合并到mJS中
          for(var id in fJS) {
            var n = fJS[id];
            if(n.state == 0) {
              if(mJS[n[this.getOption().pidkey]] != null) {
                mJS[n[this.getOption().pidkey]].cn.push(n);
                mJS[n[this.getOption().idkey]] = n;
                n.state = 0;
              } else if(fJS[n[this.getOption().pidkey]]) {
                fJS[n[this.getOption().pidkey]].cn.push(n);
                mJS[n[this.getOption().idkey]] = n;
                n.state = 1;
              } else {
                mJS[n[this.getOption().idkey]] = n;
              }
            }
          }
          var topN = [];
          for(var id in mJS) {
            if(mJS[id].state = 0) {
              topN.push(mJS[id]);
            }
          }
          
          //调用数组的Array排序功能
        }
        item = null;
        itemListJson = null;
        this.logEnd("end sort item list");
        return itemListAry;
      },
      /**
       * 设置数组数据，初始化树结构.
       * @param itemList 节点数组
       * @param callBack 回调事件
       */
      setItemList:function(itemList, callBack) {
        if(this.getAllCount() > 0) {
          this.empty(true);
        }
        if(this.isArray(itemList)) {
          this.itemList = itemList;
          itemListAry = this.sortItemList(itemList, true);
          var that = this;
          this._lockLog = true;
          this._changeSet = {"deleteList":[],"modify":{}};
          this._asynSetItemList(itemListAry, "", this.rootNode, 0, null, function(){
          that.first();
          that._lockLog = false;
          that = null;
          if(typeof callBack == "function") {
            callBack();
          }
        });
        } else {
          if(typeof callBack == "function") {
            callBack();
          }
        }
      },
      /**
       * 获取在数据模型中的下标.
       * @param {object} item 行对象
       * @return {number} index 返回下票
       * @ignore
       */
      _getDataIndex:function(item) {
        var key = this.getKeyValue(item);
        return this.dataIndexJson[key];
      },
      /**
       * 插入数组到树结构中.
       * @param itemList 数组
       * @param pNode 父节点
       * @param newCode 新的编码
       * @param callBack 回调函数
       */      
      insertItemList:function(index, itemList, pNode, newCode, callBack) {
         //初始化父节点，
        if(pNode == null) {
          pNode = this.curNode;
        }
        if(pNode.level == -1) {
          pNode.scrIndex = 0;
        }
        //展开当前节点
        pNode.expand();
        if(this.isArray(itemList)) {
          //如果是数组，则先排序
          var itemListAry = this.sortItemList(itemList, false);
          if(newCode) {
              this.newItemCode(itemListAry, pNode);
          }
          var pCode = "";
          if(pNode.level >= 0) {
            pCode = this.getValue(this.itemCode, pNode);
          }
          this._asynSetItemList(itemListAry, pCode, pNode, 0, index, callBack);
        } else {   
          //如果是对象，则直接添加
          if(typeof itemList == "object") {
              this.insertChildItem(index, itemList, pNode, newCode);
          }
          //回调函数
          if(typeof callBack == "function") {
            callBack();
          }
        }        
      },
      /**
       * 添加数组到树结构中.
       * @param itemList 数组
       * @param pNode 父节点
       * @param newCode 新的编码
       * @param callBack 回调函数
       */
      appendItemList:function(itemList, pNode, newCode, callBack) {
         //初始化父节点，
        if(pNode == null) {
          pNode = this.curNode;
        }
        if(pNode.level == -1) {
          pNode.scrIndex = 0;
        }
        //展开当前节点
        pNode.expand();
        if(this.isArray(itemList)) {
          //如果是数组，则先排序
          var itemListAry = this.sortItemList(itemList, false);
          if(newCode) {
              this.newItemCode(itemListAry, pNode);
          }
          if(this.storeData) {
            //调用增加节点扩展事件
            var cNode = pNode.getNode(pNode.getCount() - 1);
            var index = this.itemIndex;
            if(this.isEmpty(cNode)) {
              if(pNode.level < 0) {
                  index = 0;  
              } else {      
                  index = this._getDataIndex(pNode.data);
              }
            } else {
              index = this._getDataIndex(cNode.data);
              if(index == null) {
                index = this.itemIndex;
              }
            }
            this.itemIndex = index;
          }        
          var pCode = "";
          if(pNode.level >= 0) {
            pCode = this.getValue(this.itemCode, pNode);
          }
          this._asynSetItemList(itemListAry, pCode, pNode, 0, null, callBack);
        } else {   
          //如果是对象，则直接添加
          if(typeof itemList == "object") {
              this.appendChildItem(itemList, pNode, newCode);
          }
          //回调函数
          if(typeof callBack == "function") {
            callBack();
          }
        }
      },
      /**
       * 插入节点.
       * @param index 序号
       * @param item 节点
       * @param cNode 子节点
       * @param newCode 生成编码标志
       */
      insertItem:function(index, item, cNode, newCode) {
        if(cNode == null) {
          cNode = this.curNode;
        }
        var pNode = cNode.pNode;
        if(pNode == null) {
          pNode = this.rootNode;
        }
        if(index == null) {          
          if((this.sortType == "down" || this.sortType == "up")) {
        	  index = cNode.scrIndex - 1;
  		      if(this.sortType == "down") {
  		    	  index = pNode.count - index;
  		      }
          } else {
        	  index = cNode.index;
          }
        }
        this.insertChildItem(index, item, pNode, newCode);
      },
      /**
       * 插入子节点.
       * @param index 序号
       * @param item 节点
       * @param cNode 子节点
       * @param newCode 生成编码标志
       */      
      insertChildItem:function(isindex, item, pNode, newCode) {
	        if(pNode == null) {
	          pNode = this.curNode;
	        }
	        var cNode = new qzz.treenode();
	        if(item == null) {
	             item = {};
	             item[this.itemCode] = "";
	        }
	        if(newCode) {
	          item[this.itemCode] = pNode.fullcode + this.getNewCode(pNode);
	        }
	        if(isindex < 0) {
	          isindex = 0;
	        } else if(isindex == null || isindex > pNode.getCount()) {
	          isindex = pNode.getCount();
	        }
	        //设置节点信息
	      cNode.id = item[this.getOption().idkey];
	      cNode.text = item[this.getOption().capkey];
	      cNode.fullcode = String(item[this.itemCode]);
	      cNode.code = cNode.fullcode.replace(pNode.fullcode, "");
	      cNode.scrIndex = pNode.scrIndex + isindex + 1;    
	      if(this.storeData) {
	        //调用增加节点扩展事件
	          cNode.data = item;
	          //append to dataModel;
	          var index = 0;
	          if(pNode.getCount() > 0) {
	            var n = pNode.getNode(pNode.getCount() - 1);
	            if(this.isEmpty(n)) {
	              if(pNode.level < 0) {
	                index = 0;
	              } else {              
	                  index = this._getDataIndex(pNode.data);
	              }
	            } else {            
	                index = this._getDataIndex(n.data);
	            }
	          } else if(pNode.level >= 0){
	            index = this._getDataIndex(pNode.data);
	          }
	          //处理节点序号
	          this.itemIndex = index;
	          var is = [item];        
	      }
	      this.insertNode(isindex, cNode, pNode, true);
      },
      /**
       * 添加节点
       * @param item 节点
       * @param cNode 子节点
       * @param newCode 生成编码标志
       */
      appendItem:function(item, cNode, newCode) {
        if(cNode == null) {
          cNode = this.curNode;
        }
        var pNode = cNode.pNode;
        if(pNode == null) {
          pNode = this.rootNode;
        }
        this.appendChildItem(item, pNode, newCode);
      },
      /**
       * 添加子节点.
       * @param item 节点
       * @param cNode 子节点
       * @param newCode 生成编码标志
       */
      appendChildItem:function(item,  pNode, newCode) {
	      if(pNode == null) {
	          pNode = this.curNode;
	      }
	      var cNode = new qzz.treenode();
	      if(item == null) {
	           item = {};
	           item[this.itemCode] = "";
	      }
	      if(newCode) {
	        item[this.itemCode] = pNode.fullcode + this.getNewCode(pNode);
	      }
	        //设置节点信息
	      cNode.id = item[this.getOption().idkey];
	      cNode.text = item[this.getOption().capkey];
	      cNode.fullcode = String(item[this.itemCode]);
	      cNode.code = cNode.fullcode.replace(pNode.fullcode, "");
	      //如果是倒序的话就直接取1
	      if(this.sortType === "down") {
	    	  cNode.scrIndex = 1;
	      } else {
	          cNode.scrIndex = pNode.scrIndex + pNode.getCount() + 1;
	      }
	      if(this.storeData) {
	        //调用增加节点扩展事件
	          cNode.data = item;
	          //append to dataModel;
	          var index = 0;
	          if(pNode.getCount() > 0) {
	            var n = pNode.getNode(pNode.getCount() - 1);
	            if(this.isEmpty(n)) {
	              if(pNode.level < 0) {
	                index = 0;
	              } else {              
	                  index = this._getDataIndex(pNode.data);
	              }
	            } else {            
	                index = this._getDataIndex(n.data);
	            }
	          } else if(pNode.level >= 0){
	            index = this._getDataIndex(pNode.data);
	          }
	          //处理节点序号
	          this.itemIndex = index;
	          var is = [item];        
	      }
	      this.addNode(cNode, pNode, true);	  
      },
      /**
       * 删除节点.
       * @param node 要删除的节点
       */      
      delRecord:function(node) {
        return this.deleteItem(node);
      },
      /**
       * 删除节点.
       * @param node 要删除的节点
       */
      deleteItem:function(node) {
        if(node == null) {
          node = this.curNode;
        }
        var re = false;
        
        delData = null;
        //如果是叶子节点则删除
        if(node.level >= 0 && node.isLeaf()) {
          var index = node.index; //this.getIndex(node);
          var item = node.data;          
          if(this.delNode(index, node.pNode)) {
            this._hasDelete = true;
            re = true;
          }
        }
        return re;
      },
      /**
       * 获取节点编号.
       * @return 编号
       */
      getRecNo:function() {
        return this.curNode.recNo;
      },
      /**
       * 获取节点全局序号.
       * @return 序号
       */
      getIndex:function(node) {
        if(node == null) {
          node = this.curNode;
        }
        return node.scrIndex;
      },
      /**
       * 获取显示文本.
       * @return 文本 
       */
      getDisplay:function(node) {
    	  if(node == null) {
    		  node = this.getCurNode();
    	  }
          return node.text;
      },
      /**
       * 判断字符串是串中是否存在.
       */
      indexOfText:function(text) {
    	  if(text == null || text === "") return null;
    	  this.savemark();
    	  this.first();
    	  while(!this.isEof()) {
    		  var cnode = this.getCurNode();
    		  if(text === cnode.text) {
    			  this.loadmark(false);
    			  return cnode;
    		  }
    		  this.next(true);
    	  }
    	  this.loadmark(false);
    	  return null;
      },
      /**
       * 定位节点.
       * @param text 定位字符串
       * @param fieldName 字段名
       * @return 状态
       */
      locate:function(text, fieldName, full, checkCrt) {
        full = this.getBooleanValue(full, false);
        var cn = this.getCurNode();
        var hasFound = false;
        text = text + "";
        text = this.trim(text);
        if(fieldName == "@INDEX") {
            //序号定位
          var ui = this.beginUpdate(true);
          this.first();
          while(!this.isEof()) {
            var dv = this.getIndex();
            if(text == dv) {
              hasFound = true;
              break;
            }
            this.next();
          }
          this.endUpdate(false, ui);
        } else {
          var ui = this.beginUpdate(true);
          if(checkCrt === true) {
            var dv = this.getDisplay(fieldName) + "";
            var dv2 = this.getValue(fieldName) + "";
            if(full && (dv == text || dv2 == text) || !full && (dv.indexOf(text) >= 0 || dv2.indexOf(text) >= 0)) {
              hasFound = true;
            }
          }
          if(hasFound !== true) {
            if(!this.isExpand()) {
              this.expand();
            }
            this.next();
              //从当前节点开始循环判断查找 
            while(!this.isEof()) {
              var dv = this.getDisplay(fieldName) + "";
              var dv2 = this.getValue(fieldName) + "";
              if(full && (dv == text || dv2 == text) || !full && (dv.indexOf(text) >= 0 || dv2.indexOf(text) >= 0)) {
                hasFound = true;
                break;
              }
              if(!this.isExpand()) {
                this.expand();
              }
              this.next();
            }
            if(this.isEof()) {
              //如果找不到则从起始节点开始循环判断查找
              this.first();
              while(!this.isEof() && this.getIndex() < cn.getIndex()) {
                var dv = this.getDisplay(fieldName) + "";
                var dv2 = this.getValue(fieldName) + "";
                if(full && (dv == text || dv2 == text) || !full && (dv.indexOf(text) >= 0 || dv2.indexOf(text) >= 0)) {
                  hasFound = true;
                  break;
                }
                if(!this.isExpand()) {
                  this.expand();
                }
                this.next();
              }
            }      
          }
          this.endUpdate(false, ui);
        }
        return hasFound;
      },
      
      /**
       * 异步处理全展开函数.
       * @param limit 限制数量
       * @param node 节点
       * @param callback 回调
       */
      _asynExpandToLevel:function(limit, node, level, callback) {
        this.logInfo("_asynFullExpand:" + node.recNo);
        var i = 0;
        var ui = this.beginUpdate(true);
        var cNode = this.curNode;
        //设置起始节点
        this.select(node);
        //循环处理所有节点 
        while(this.next(true)) {
          //如果层次小于等于level, 且收缩的，则展开
          if(this.curNode.level <= level) {
            this.curNode.expand();
          } else if(this.curNode.expanded) {
            //否则，如果展开的状态，则收缩
            this.getCurNode().collapse();            
          }
          i++;
          if(i >= limit) {
            break;
          }
        }
        node = this.curNode;
        i = null;
        if(!this.isEof()) {
          //如果还没有处理结束，则异步再调用自己.
          this.select(cNode);
          this.endUpdate(false, ui);
          var _this = this;
          setTimeout(function(){_this._asynExpandToLevel(limit, node, level, callback); _this = null},10);
        } else {
          //如果处理结束，则调用回调函数
          this.select(cNode);
          this.endUpdate(false, ui);
          if(typeof callback == "function") {
            callback();
          }
        }
      },
      /**
       * 展开到指定层次.
       * @param level 展开层次
       */
      expandToLevel:function(level, callBack) {
        if(this.isEmpty(level)) {
          level = 0;
        } else if(typeof level == "string" && !isNaN(level)) {
          level = parseInt(level, 10);
        } else if(isNaN(level)){
          if(typeof callBack == "function") {
            callBack();
          }
          return false;
        }
        this._asynExpandToLevel(1000, this.rootNode, level, callBack);
        return true;
      },
    /**
     * 选择所有节点.
     * @name selectAll
     * @param {qzz.treenode} node 跟节点
     * @param {function} callBack 回调整函数 
     */
    checkAll:function(node, callBack) {
      if(typeof node === "function") {
         callBack = node;
         node = null;
      }       
      this.each(node, function(cn) {
        if(cn.enable !== false) {
              this.setCheck(cn)
        }
      }, callBack);
    },
    selectAll:function(node, callBack) {
      this.checkAll(node, callBack);
    },
    /**
     * 取消选择所有节点.
         * @name unCheckAll
     * @param {qzz.treenode} node 跟节点
     * @param {function} callBack 回调整函数 
     */
    unCheckAll:function(node, callBack) {
      if(typeof node === "function") {
         callBack = node;
         node = null;
      }
      this.each(node, function(cn) {
        if(cn.enable !== false) {
             this.setUnCheck(cn)
        }
      }, callBack);
    },
    unSelectAll:function(node, callBack) {
       this.unCheckAll(node, callBack);
    },
    /**
     * 反向选择所有节点.
     * @param node 跟节点
     * @param callBack 回调整函数 
     */
    reverCheckAll:function(node, callBack) {
      if(typeof node === "function") {
         callBack = node;
         node = null;
      }
      this.each(node, function(cn) {
        if(cn.checkState) {
          this.setUnCheck(cn);
        } else {        
            this.setCheck(cn)
        }
      }, callBack);
    },
    reverSelectAll:function(node, callBack) {
        this.reverCheckAll(node, callBack);
     },
     /**
      * 返回主键keyName.
      */
     getKeyName:function() {
        if(typeof this.keyName === "string") {
          return this.keyName;
       } else if(this.isArray(this.keyName)) {
          this.keyName.join("_");
       }
     },
     /**
      * 获取主键值.
      * item 主键json对象.
      */
     getKeyValue:function(item) {
        if(typeof item == "object") {
          if(typeof this.keyName == "string") {
            return item[this.keyName];
          } else if(this.isArray(this.keyName)) {
            var re = [];
            for(var i = 0, len = this.keyName.length; i < len; i++) {
              re[i] = item[this.keyName[i]];
            }
            return re.join("_");
          }
        }
     },
     /**
      * 设置节点是否可复选.
      */
     setEnableCheck:function(state, node) {
       if(node == null) {
         node = this.curNode;
       }
       node.enable = state;
     },
     setEnableSelect:function(state, node) {
       this.setEnableCheck(state, node);
     },    
     
         /**
      * 循环明细.
      * @param {function} eachFunc 执行体函数
      * @param {boolean} desc 是否倒序
      * @param {number} limit 异步数量
      * @param {treeNode} node 根节点
      * @param {treeNode} cNode 当前节点
      * @param {function} callBack 回调函数
      */
     _asynEachFunc:function(eachFunc, desc, limit, node, cNode, callBack) {
       this.logInfo("_asynEachFunc:");
       var i = 0;
       var ui = this.beginUpdate(true);
       //原来保存的行
       var tmpNode = this.curNode;
       //设置起始节点
       this.select(cNode);
       var isContinue = true;
       var index = 0;
       if(desc) {         
         //循环处理所有节点 
         while(!this.isBof() && i < limit && (this.getLevel() > node.level || this.getRecNo() == node.recNo)) {
            //如果层次小于等于level, 且收缩的，则展开
          this._hasDelete = false;
             var cn = this.getCurNode();
             index = this.getIndex();
             try {
               var re = eachFunc.apply(this, [cn]);
               if(re === "break" || re === "Break" || re === false) {
                 isContinue = false;
                 break;
               }
             } catch(e) {
                 this.logInfo("[EACH ERROR!]:" + e.message);  
                 isContinue = false;
             }             
             cn = null;
             //如果被删除了，行数等于1时，会自动往后移，行数大于1的时候，自动会往前移，
             if(!this._hasDelete) {
               this.prior(true);
             }       
             i++;
         }
         isContinue = isContinue && !this.isBof() && (this.getLevel() > node.level || this.getRecNo() == node.recNo);
       } else {         
           //如果是全循环时，传入的是rootNode.
           if(this.curNode.level < 0) {
             this.next(true);
           }
           //循环处理所有节点 
         while(!this.isEof() && i < limit && (this.getLevel() > node.level || this.getRecNo() == node.recNo)) {
            //如果层次小于等于level, 且收缩的，则展开
          this._hasDelete = false;
             var cn = this.getCurNode();
             index = this.getIndex();
             try {
               var re = eachFunc.apply(this, [cn]);
               if(re === "break" || re === "Break" || re === false) {
                 isContinue = false;
                 break;
               }
             } catch(e) {
                 this.logInfo("[EACH ERROR!]:" + e.message);  
                 isContinue = false;
             }
             cn = null;
             //如果被删除了，行数等于1时，会自动往后移，行数大于1的时候，自动会往前移，
             if(this._hasDelete &&　this.getAllCount() > 0) {
               if(index > 1) {
                 this.next(true);
               }
             } else {
               this.next(true);
             }           
             i++;
         }
         isContinue = isContinue && !this.isEof() && (this.getLevel() > node.level || this.getRecNo() == node.recNo); 
       }       
       cNode = this.curNode;
       i = null;
       if(isContinue) {
           //如果还没有处理结束，则异步再调用自己.
        if(tmpNode._hasDestroy !== true) {
               this.select(tmpNode);
        }
           this.endUpdate(false, ui);
           var _this = this;
           setTimeout(function(){_this._asynEachFunc(eachFunc,desc, limit, node, cNode, callBack); _this = null},10);
       } else {
           //如果处理结束，则调用回调函数
        if(tmpNode._hasDestroy !== true) {
               this.select(tmpNode);
        }
           this.endUpdate(false, ui);
           if(typeof callBack == "function") {
             callBack.apply(this);
           }
       }   
     },
     /**
      * 循环所有选择节点.
      * @param {treeNode} node 根节点， 可不传
      * @param {function} eachFunc 循环体参数， node
      * @param {boolean} desc 是否反身循环
      * @param {function} callBack 回调函数
      */
     eachChecked:function(node, eachFunc, desc, callBack) {
       if(this.multiSelect) {
         var nt = typeof node;
         var dt = typeof desc;
         if(nt === "function") {
           var et = typeof eachFunc;
           if(et === "function") {
             desc = false;           
             callBack = eachFunc;
           } else if(et === "boolean") {
             if(dt === "function") {
               callBack = desc;
             }
             desc = eachFunc;
           }
           eachFunc = node;
           node = this.rootNode;
         } else if(nt == "object") {         
           if(dt === "function") {
             callBack = desc;
             desc = false;
           }           
         }
           if(desc) {
             //获得node的最后一个子节点
             var cNode = this.lastNode(node);
           } else {
             cNode = node;
           }
         this._asynEachFunc(function(node){
            if(this.isChecked()) {
              return eachFunc.apply(this, [node]);
            }
                  }, desc, 100, node, cNode, callBack);
        }//if
     },
     eachSelected:function(node, eachFunc, desc, callBack) {
       this.eachChecked(node, eachFunc, desc, callBack);
     },
     /**
      * 循环处理.
      * @param {treeNode} node 根节点， 可不传
      * @param {function} eachFunc 循环体参数， node
      * @param {boolean} desc 是否反身循环
      * @param {function} callBack 回调函数
      */     
     forEach:function(node, eachFunc, desc, callBack) {
      this.each(node, eachFunc, desc, callBack);
     },
     /**
      * 循环处理.
      * @param {treeNode} node 根节点， 可不传
      * @param {function} eachFunc 循环体参数， node
      * @param {boolean} desc 是否反身循环
      * @param {function} callBack 回调函数
      */
     each:function(node, eachFunc, desc, callBack) {
       var nt = typeof node;
       var dt = typeof desc;
       if(nt === "function") {
         var et = typeof eachFunc;
         if(et === "function") {
           desc = false;           
           callBack = eachFunc;
         } else if(et === "boolean") {
           if(dt === "function") {
             callBack = desc;
           }
           desc = eachFunc;
         }
         eachFunc = node;
         node = this.rootNode;
       } else if(nt == "object") {         
         if(dt === "function") {
           callBack = desc;
           desc = false;  
         }                
       }
         if(desc) {
           //获得node的最后一个子节点
           var cNode = this.lastNode(node);
         } else {
           if(node == null) {
             node = this.rootNode;
           }
           cNode = node;
         }
         this._asynEachFunc(eachFunc, desc, 100, node, cNode, callBack);
     },
     /**
      * 获取节点是否只读.
      * @name getEnableCheck
      * @param {qzz.treenode} node
      * @reutrn {boolean} 返回当前节点，是否可以选择
      */
     isEnableCheck:function(node) {
       if(node == null) {
         node = this.curNode;
       }
       return node.enable;
     },
     getEnableSelect:function(node) {
       return this.isEnableCheck(node);
     },
     /**
      * 返回选择的节点.
      */
     getCheckedData:function() {
       var cn = this.getCurNode();
       this.first();
       var selDatas = [];
       while(!this.isEof()) {
         if(this.isChecked()) {
           selDatas.push(this.getCurNode());
         }
         this.next(true);
       }
       return selDatas;
     },
     getSelectedData:function() {
       return this.getCheckedData();
     },
     /**
      * 通过id设置选择值.
      * @param txts id列表
      * @param callBack 回调函数
      */
     setSelectById:function(txts, callBack) {
    	 if(this.multiSelect === true) {
    		 var re = [];
	    	 var txtAry = txts;
	    	 if(typeof txtAry == "string") {
	    		txtAry = txtAry.split(",");
	    	 }
	    	 if(txtAry.length > 0) {    		
		    	 var txtJson = {};
		    	 for(var i = 0, ilen = txtAry.length; i < ilen; i++) {
		    		txtJson[txtAry[i]] = true;
		    	 }
		    	 this.each(function(node) {
		    		 if(this.isEnableCheck(node) === true) {
		    			 var id = this.getId(node);
		    			 if(txtJson[id] === true) {
		    				 if(this.isChecked(node) !== true) {
		    			         this.setCheck(node);
		    				 }
		    			     re.push(node);
		    			 }
		    		 }
		    	 }, function() {
		    		 if(typeof callBack == "function") {
		    		     callBack(re);		    		 
		    		 }
		    	 });
	    	 } else if(typeof callBack == "function") {
	    		 callBack(re);
	    	 }
    	 } else {
    		 this.locate(txts, this.getOption().idkey, true, true);
    		 if(typeof callBack == "function") {
    		     callBack([this.getCurNode()]);		    		 
    		 }
    	 }
     },
     /**
      * 通过text设置选择值.
      * @param txts text列表
      * @param callBack 回调函数
      */
     setSelectByText:function(txts, callBack) {
    	 if(this.multiSelect === true) {
    		 var re = [];
	    	 var txtAry = txts;
	    	 if(typeof txtAry == "string") {
	    		txtAry = txtAry.split(",");
	    	 }
	    	 if(txtAry.length > 0) {  
		    	 var txtJson = {};
		    	 for(var i = 0, ilen = txtAry.length; i < ilen; i++) {
		    		txtJson[txtAry[i]] = true;
		    	 }
		    	 this.each(function(node) {
		    		 if(this.isEnableCheck(node) === true) {
		    			 var id = this.getText(node);
		    			 if(txtJson[id] === true) {
		    				 if(this.isChecked(node) !== true) {
		    			         this.setCheck(node);
		    				 }
		    			     re.push(node);
		    			 }
		    		 }
		    	 }, function() {
		    		 if(typeof callBack == "function") {
		    		     callBack(re);		    		 
		    		 }
		    	 });
	    	 } else if(typeof callBack == "function") {
	    		 callBack(re);
	    	 }
    	 } else if(txts != null && txts !== ""){
    		 this.locate(txts, this.getOption().capkey, true, true);
    		 if(typeof callBack == "function") {
    			 callBack([this.getCurNode()]);
    		 }
    	 }
     },
     /**
	  * 清空过滤条件.
	  */
	 _emptyFilterItem:function() {
	     this.filterStack = [];
	 },
	 /**
	  * 清除过滤条件.
	  */
     clearFilterText:function(callBack) {
    	 if(this.filtered) {
	         this._emptyFilterItem();
		     this.setFiltered(false, callBack);
    	 } else if(typeof callBack == "function") {
    		 callBack();
    	 }
     },
     /**
      * 添加过滤条件.
      */
     addFilterText:function(selItem, filter, callBack) {
    	 if(typeof filter == "function") {
    		 callBack = filter;
    		 filter = false;
    	 }
    	 if(typeof selItem == "string") {
    		 selItem = {sel:selItem};
    	 }
    	 this.filterStack.push(selItem);
    	 if(filter) {
    		 this.setFiltered(filter, callBack);
    	 } else if(typeof callBack == "function") {
    		 callBack();
    	 }    	 
     },
	 /**
	  * 执行过滤
	  */
	  doFilter:function(callBack) {
	  	if(this.filtered) {
	   		var _this = this;
	   		this._asynResetFilterNode(undefined, undefined, 1000, true, function() {
	   			_this.filtered = true
	   			if(_this.getAllCount() > 0) {
	   			    _this._asynFilterNode(undefined, undefined, 500, true, undefined, callBack);
	   			} else if(typeof callBack === "function") {
	   				callBack();
	   			}
	   		});
	   	} else {	
	   		this.filtered = true;
	   		if(this.getAllCount() > 0) {	    			
	   	        this._asynFilterNode(undefined, undefined, 500, true, undefined, callBack);
	   		} else if(typeof callBack === "function") {
 			    callBack();
 		    }
	   	}
	    	//this.filtered = true;
	 },
     /**
      * 执行过滤.
      * @name setFiltered
      * @param filtered 执行过滤
      * @param callBack 回调函数
      */
     setFiltered:function(filtered, callBack) {
    	 var _this = this;
    	 if(filtered) {
    		 if(this.hasbind("onServerFilter")) {
    			this.trigger("onServerFilter",[filtered, this.filterStack, callBack]);
    		 } else {
	    		//执行过滤
		        this.doFilter(function() {
		        	_this.first();
		        	_this.getCurNode().scrIndex = 1;		        	
		        	if(typeof callBack === "function") {
		        	    callBack();	
		        	}
		        });
    		 }
    	 } else {
    		if(this.hasbind("onServerFilter")) {
	    			this.trigger("onServerFilter",[filtered, this.filterStack, callBack]);
	    	} else {
		    	this._asynResetFilterNode(undefined, undefined, 1000, true, function() {
		    		_this.filtered = false;
		    		_this.first();
		    		if(typeof callBack === "function") {
			       	    callBack();	
			       	}
		    		
		    	});
	    	}
    	 }
     },
     /**
      * 获取当前行的过滤状态.
      */
     _getFilterState:function(node) {
        if(node == null) {
          node = this.curNode;
        }
        var state = true;
        for(var i = 0, len = this.filterStack.length; i < len && state; i++) {
          var item = this.filterStack[i];
          var sel = item.sel;
          var val = node.text || "";
          var val2 = node.id || "";
          //添加默认空白符
          if(val === "") {
            val = "("+this.getI18n("qzz.empty", "空白")+")";
          }
          if(typeof val !== "string") {
        	  val += "";
          }
          if(typeof val2 !== "string") {
        	  val2 += "";
          }
          //包含关系
          if(val2.indexOf(sel) == -1 && val.indexOf(sel) == -1) {
            state = false;
          }
//          if(sel[val] == null && sel[val2] == null) {
//            state = false;
//          }
          item = null;
        }
        return !state;
     },
     /**
      * 还原非过滤状态.
      * @param fieldName 字段
      * @param node 起始节点
      * @param cNode 当前节点
      * @param limitCount 分批执行次数
      * @param fullCompare 全匹配
      * @param callBack 回调函数
      */
      _asynResetFilterNode:function(node, cNode, limitCount, fullCompare, callBack) {
          this.logInfo("_asynResetFilterNode:");
          if(node == null) {
            node = this.rootNode;
            node.loadState();
          }
          if(cNode == null) {
            cNode = this.getChildNode(0, node);//node.cNode[0];
          }
          var tmpNode = this.getCurNode();
          var ui = this.beginUpdate(true);
          this.select(cNode);
          var i = 0;
          while(!this.isEof() && i < limitCount && this.getLevel() > node.level) {   
            this.getCurNode().loadState();
            this.next(true);          
            i++;
          }
          cNode = this.getCurNode();
          if(!this.isEof() && this.getLevel() > node.level) {
            var _this = this;
            this.select(tmpNode);
            this.endUpdate(false, ui);
            setTimeout(function(){_this._asynResetFilterNode(node, cNode, limitCount, fullCompare, callBack); _this = null;}, 10);
          } else {
            this.select(tmpNode);
            this.endUpdate(false, ui);
            if(typeof callBack === "function") {
              callBack.apply(this);
            }            
          }  
     },
     /**
      * 异步执行过滤.
      * @param {treeNode}node 起始节点
      * @param {treeNode}cNode 当前节点
      * @param {number}limitCount 分批执行次数
      * @param {boolean}fullCompare 全匹配
      * @param {Array.treenode}pNodeStack 父节点堆栈
      * @param {function}callBack 回调函数
      */
     _asynFilterNode:function(node, cNode, limitCount, fullCompare, pNodeStack, callBack) {
          this.logInfo("_asynFilterNode:");
          if(node == null) {
            node = this.rootNode;
            node.saveState();
          }
          var pnStack = pNodeStack;
          if(pnStack == null) {
            pnStack = [];
          }
          var cn = cNode;
          //顶点排序状态去除.
          node.sortState = {};
          if(cn == null) {
            cn = this.getChildNode(0, node);//node.cNode[0];
          }
          var tmpNode = this.getCurNode();
          var ui = this.beginUpdate(true);
          this.select(cn);
          var i = 0;
          var top = pnStack.length;
          var isPop = false;
          while(!this.isEof() && i < limitCount && this.getLevel() > node.level) {   
            cn = this.getCurNode();     
            isPop = false;
            //判断，非全过滤时，如查栈中有父节点，则出栈
            if(!this._filterAll && top > 0 && cn.level <= pnStack[top - 1].level) {
              cn = pnStack.pop();
              this.select(cn);
              isPop = true;
              top--;
            }
              //排序状态重置          
              var fs = cn.filterState;
              if(!fs && isPop == false) {
                cn.saveState();
              }
            if(!this._filterAll && cn.getCount() > 0) {
              //如果有子节点，并且
              pnStack.push(cn);
              top++;
              fv = false;            
            } else {
              //获取过滤结果
                var fv = this._getFilterState(cn);
                //过滤扩展事件,
                if(this.hasbind("onFilter")) {
                    fv = this.trigger("onFilter", [this, fv]);
                }
                //如果有一个没有过滤成功，则其所有父节点都不过滤了。
                if(!this._filterAll && !fv) {
                  pnStack = [];
                top = 0;
              }
            }
            cn.sortState = {};
            //先移动到下一个节点.
            this.next(!fv);
            //执行删除处理.
            if(fv) {
              var pNode = cn.pNode;
              //因为删除后，后续的index会发生变化，因此先保存.
              if((!fs || isPop) && pNode.getCount() > cn.index) {
                for(var j = cn.index + 1; j < pNode.getCount(); j++) {
                  pNode.cNode[j].saveState();
                }
              }
              if(tmpNode != null && tmpNode.recNo == cn.recNo){
                tmpNode = null;
              }
              //排序状态重置
              cn.pNode.delNode(cn.index, false);
            }  
            if(this.isEof() && top > 0) {
              cn = pnStack.pop();
              this.select(cn);
              top--;
            }
            i++;
          }
          cn = this.getCurNode();
          if(!this.isEof() && this.getLevel() > node.level) {
            if(tmpNode != null) {
                this.select(tmpNode);
            }
            this.endUpdate(false, ui);
            var _this = this;            
            setTimeout(function(){_this._asynFilterNode(node, cn, limitCount, fullCompare, pnStack, callBack); _this = null;}, 10);
          } else {
            if(tmpNode != null) {
                this.select(tmpNode);
            }
            this.endUpdate(false, ui);
            pnStack = null;
            if(typeof callBack === "function") {
              callBack.apply(this);
            }
          }  
     },
    /**
     * 获得changedSet.
     */
    getChangeSet:function() {
       var csAry = [];
       csAry = csAry.concat(this._changeSet.deleteList);
       for(var key in this._changeSet.modify) {
    	   var mn = this._changeSet.modify[key];
    	   if((mn.modelState & this.modelState.NEW) > 0) {
    		   mn.data._index = mn.index;
    	   }
           csAry.push(mn.data);
       }
       return csAry;
    },
    /**
     * 兼容ECP变更集
     */
    getEcpChangeSet:function() {
      var wrapList = {};
      var csAry = [];
      //收集变更数据，以及旧值.
      for(var key in this._changeSet.modify) {
        var cn = this._changeSet.modify[key];
          csAry.push(cn.data);
      }
          wrapList.list = csAry;
          if(this._changeSet.deleteList.length > 0) {
              wrapList.deleteList = this._changeSet.deleteList;
          }
      return wrapList;
    },
        /**
         * 合并日志.
         */
        mergeChangeLog:function() {
          this._changeSet.deleteList = [];
          for(var key in this._changeSet.modify) {
        this._changeSet.modify[key].data.modelState = this.modelState.DEFAULT;  
        this._changeSet.modify[key].data._oldValue = {};
      }
          this._changeSet.modify = {};
        },
        /**
         * 是否叶子节点.
         */
        isLeaf:function(node) {
          if(node == null) {
            node = this.getCurNode();
          }
          return node.isLeaf();
        },
        /**
         * 多行移动.
         */
        mulMoveNode:function(step, callBack) {
          var fnode = [];
          var desc = step >= 0;
          var levelStep = {};
          this.eachChecked(function(node) {
            fnode.push(node);
            if(levelStep[node.level] == null) {
              levelStep[node.level] = step;
            }
            var pNode = node.pNode;
            if(desc === true 
                && node.index + 1 < pNode.count
                    && pNode.cNode[node.index + 1].checkState !== true
               || desc === false 
                    && node.index > 0 
                        && pNode.cNode[node.index - 1].checkState !== true) {
                var re = this.moveNode(levelStep[node.level], node, true);
            }
        if(re.toIndex != re.fromIndex) {
          levelStep[node.level] = re.toIndex - re.fromIndex;
        }
          }, desc, function(){
            if(typeof callBack == "function") {
              callBack(step, fnode);              
            }
          });
        },
        /**
         * 移动当前节点.
         * @param step 移动的位置数量
         * @param cNode 移动的节点
         * @param lockCurrent 当前节点所以的行
         */
        moveNode:function(step, cNode, lockCurrent) {
        if(this.isEmpty(step)) {
          step = 1;
        }
        //处理移动大小
        if(typeof step == "string") {
          if(!isNaN(step)) {
            step = parseInt(step, 10);
          } else {
            step = 1;
          }
        }
        //获得当前节点.
        //var cNode = this.getCurNode();
        if(cNode == null) {
          cNode = this.getCurNode();
        }
        var pNode = cNode.pNode;
        var index = cNode.index;
        var cScrIndex = cNode.scrIndex;
        //计算需要替换的节点
        var nIndex = index + step;
        //把nIndex范围限定在数组内.
        if(nIndex < 0) {
          nIndex = 0;
        } else if(nIndex > pNode.getCount() - 1) {
          nIndex = pNode.getCount() - 1;
        }
        if(index != nIndex) {          
            var tNode = pNode.getNode(nIndex);
            var nScrIndex = tNode.scrIndex;
            //往前移
            if(index > nIndex) {
            for(var i = index; i > nIndex; i--) {
              pNode.cNode[i - 1].scrIndex++;
              pNode.cNode[i - 1].index = i;
              pNode.cNode[i] = pNode.cNode[i - 1];
            }
          } else {
            //往后移
            for(var i = index; i < nIndex; i++) {
              pNode.cNode[i + 1].scrIndex--;
              pNode.cNode[i + 1].index = i;
              pNode.cNode[i] = pNode.cNode[i + 1];
            }
          }
          cNode.scrIndex = nScrIndex;
          cNode.index = nIndex;
            pNode.cNode[nIndex] = cNode;
            
            if(lockCurrent === true) {
            if(index > 0) {
              var ncn = pNode.getNode(index);
              this.select(ncn);
            }
          }
        }
        return {"fromIndex":index, "toIndex":nIndex};
      },
      savemark:function() {
        this._markNode = this.curNode;
        this.beginUpdate();
      },
      loadmark:function(state) {
        this.select(this._markNode);
        this.endUpdate(state === true);
      },
     /**
      * 注析掉当前类.
      * @ignore
      */
     destroy:function() {
         // 复制静态方法
       this.empty();
       this.base();
     }   
          
  });
  return qzz.treelist;
});